<?php
require_once("../../../config.inc.php");
$isPopUp = true;
require_once("../../../app/_inc/include.php");

$errMsg = "";
$process_unit = "";
$type = "";
$contentType = "";
$docContent = "";
$fileName = "";
if(isset($_REQUEST["pu"])){
	$process_unit = $_REQUEST["pu"];
}else{
	$errMsg = _t("Il processo non &egrave; definito")."<br>";
}
if(isset($_REQUEST["fn"])){
	$fileName = $_REQUEST["fn"];
}else{
	$errMsg = _t("Il file da visualizzare non &egrave; definito")."<br>";
}
if($errMsg == ""){
	//scarico il documento
	//$contentType = "application/pdf";
	//$contentType = "application/force-download";
	$sSQL = "SELECT mime_type FROM process_unit_doc WHERE process_unit='$process_unit' AND file_name='$fileName'";
	
	if($objSDAO->query($sSQL, "doc")){
		if($row = $objSDAO->fetch("doc")){
			$contentType = $row["mime_type"];
		}else{
			$errMsg .= _t("Impossibile accedere al documento richiesto!")."<br>";
		}
	}else{
		$errMsg .= $sSQL." - ".$objSDAO->error;
	}	
}

if($errMsg == ""){
	$dir = SERVER_FS_DOC."/mb_module";
	$dir .= "/".substr($process_unit,0,4);
	$dir .= "/".substr($process_unit,5,4);
	$dir .= "/".substr($process_unit,10);			
	if(file_exists($dir."/$fileName")){
		$fh = fopen($dir."/$fileName", "rb");
		$docContent = stream_get_contents($fh); 
		fclose($fh);
	}else{
		$errMsg .= "Il file richiesto &egrave; inesistente";
	}
}	
if($errMsg == ""){
	header("Content-type: $contentType");
	//header("Content-Disposition: attachment; filename=$fileName");
	echo($docContent);
}else{
	//output HTML
	$aOpz = array("tit2"=>_t("Scarica documento"));
	echo(headerHTML($aOpz));
	echo(bodyHTML($aOpz));
	//output comandi
	$aCmd = array(	"close"=>array("pag"=>"javascript:self.close()",
											"title"=>_t("chiudi la finestra"),
											"text"=>_t("chiudi")),
						
						);
	echo(commandsHTML($aCmd));	
	
	echo(footerHTML());
	$objSDAO->close();
}
?>